{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1671dee4-9ac6-4baa-bcbc-2f708493ded9",
   "metadata": {},
   "source": [
    "# Arithmetic Expressions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95af82ec-954f-4321-924a-352064eee8f4",
   "metadata": {},
   "source": [
    "This tutorial demonstrates automatic arithmetic operation management by the synthesis engine. It synthesizes a complex arithmetic expression, where uncomputation procedure, together with initialization and reuse of auxiliary qubits, are all automated. Given different **global** width or depth constraints results in different circuits."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9832b996-79c6-4f6d-8b49-6faac1891188",
   "metadata": {},
   "source": [
    "Define a quantum model that applies some quantum arithmetic operation on `QNum` variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "674d1423-6ac5-4f6b-8328-7073a72c0a28",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:00:16.789628Z",
     "iopub.status.busy": "2024-05-07T14:00:16.788315Z",
     "iopub.status.idle": "2024-05-07T14:00:19.713422Z",
     "shell.execute_reply": "2024-05-07T14:00:19.712748Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import Output, QBit, QNum, prepare_int, create_model, qfunc, synthesize, execute\n",
    "from classiq.qmod.symbolic import max\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(z: Output[QNum]):\n",
    "    x = QNum(\"x\")\n",
    "    y = QNum(\"y\")\n",
    "    prepare_int(2, x)\n",
    "    prepare_int(1, y)\n",
    "    z |= (2 * x + y + max(3 * y, 2)) > 4\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50dfe506-af0c-4300-a4b4-064b14567985",
   "metadata": {},
   "source": [
    "You can try different optimization scenarios, below we introduce two examples:\n",
    "1. Optimizing over depth and constraining the maximal width to 9 qubits.\n",
    "2. Optimizing over depth and constraining the maximal width to 12 qubits."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5474a102",
   "metadata": {},
   "source": [
    "Optimizing over depth and constraining the maximal width to 9 qubits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ba0e933a-9878-4ce2-b5af-2e4c65638f60",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:00:19.716687Z",
     "iopub.status.busy": "2024-05-07T14:00:19.715919Z",
     "iopub.status.idle": "2024-05-07T14:00:26.267367Z",
     "shell.execute_reply": "2024-05-07T14:00:26.266648Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/737ef5dd-eba0-49f5-9a61-25814ca2f523?version=0.42.0\n",
      "The result of the arithmetic calculation:  [{'z': 1.0}: 2048]\n"
     ]
    }
   ],
   "source": [
    "from classiq import (\n",
    "    Constraints,\n",
    "    Preferences,\n",
    "    set_constraints,\n",
    "    set_preferences,\n",
    "    show,\n",
    "    write_qmod,\n",
    ")\n",
    "\n",
    "constraints = Constraints(optimization_parameter=\"depth\", max_width=9)\n",
    "\n",
    "preferences = Preferences(random_seed=424788457)\n",
    "qmod = set_constraints(qmod, constraints)\n",
    "qmod = set_preferences(qmod, preferences)\n",
    "\n",
    "qprog = synthesize(qmod)\n",
    "show(qprog)\n",
    "\n",
    "job = execute(qprog)\n",
    "result = job.result()\n",
    "parsed_counts = result[0].value.parsed_counts\n",
    "print(\"The result of the arithmetic calculation: \",parsed_counts)\n",
    "\n",
    "\n",
    "write_qmod(qmod, \"arithmetic_demo_9_qubits\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2397fbdd-3a20-4e8f-bc53-6ff7b588b7ee",
   "metadata": {},
   "source": [
    "Change the quantum model constraint to treat the second scenario for optimizing over depth and constraining the maximal width to 12 qubits: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "77d24a43-5114-46a0-8309-33af8fdc75fb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:00:26.272358Z",
     "iopub.status.busy": "2024-05-07T14:00:26.271131Z",
     "iopub.status.idle": "2024-05-07T14:00:33.694219Z",
     "shell.execute_reply": "2024-05-07T14:00:33.693482Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/00705127-ab37-41d3-b8a6-71939bf39146?version=0.42.0\n",
      "The result of the arithmetic calculation:  [{'z': 1.0}: 2048]\n"
     ]
    }
   ],
   "source": [
    "constraints = Constraints(optimization_parameter=\"depth\", max_width=12)\n",
    "qmod = set_constraints(qmod, constraints)\n",
    "\n",
    "qprog = synthesize(qmod)\n",
    "show(qprog)\n",
    "\n",
    "job = execute(qprog)\n",
    "result = job.result()\n",
    "parsed_counts = result[0].value.parsed_counts\n",
    "print(\"The result of the arithmetic calculation: \",parsed_counts)\n",
    "\n",
    "\n",
    "write_qmod(qmod, \"arithmetic_demo_12_qubits\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad94a769",
   "metadata": {},
   "source": [
    " ### Mathematical Background\n",
    " \n",
    " The given mathematical expression:\n",
    " $$z = (2 \\cdot x + y + \\max(3 \\cdot y, 2)) > 4$$ \n",
    " is solved by the automatic arithmetic operation management, optimizing over depth and constraining the maximal width to 9 and 12 qubits, which outputs the value for z.\n",
    " \n",
    " __The `parsed_counts` result:__\n",
    " - Optimizing over depth and constraining the maximal width to 9 qubits: `[{'z': 1.0}: 2048]`\n",
    " - Optimizing over depth and constraining the maximal width to 12 qubits: `[{'z': 1.0}: 2048]`\n",
    "\n",
    "Both the result are same which verifies the arithmetic expression to be True.\n",
    " \n",
    " __The expected result__:<br>\n",
    " \n",
    " Allocated values:\n",
    " \n",
    " $$x = 2\\\\ y = 1$$\n",
    " \n",
    " \n",
    " Expression Calculation:\n",
    "\n",
    " $$2 \\cdot x + y = 2 \\cdot 2 + 1 = 4 + 1 = 5 \\\\ 3 \\cdot y = 3 \\cdot 1 = 3 \\\\ \\max(3 \\cdot y, 2) = \\max(3, 2) = 3$$\n",
    " \n",
    " Therefore:\n",
    " \n",
    " $$2 \\cdot x + y + \\max(3 \\cdot y, 2) = 5 + 3 = 8$$\n",
    " \n",
    " As $$8 > 4 \\implies \\text{True}$$\n",
    " Z is assigned the value True : $Z \\implies 1$"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
